<?php
class Solr_Util_PageBar{

	/**
	 * 输出分页栏
	 *
	 * @param string $AScriptName 要进行滚动的页面
	 * @param integer $ATotalCount 滚动信息的总数
	 * @param integer $ANowPageID 当前页号
	 * @param integer $AInnerCount 每页内的信息数
	 * @param string $AVar 需要附带的其它变量及其值
	 * @param string $APageKey 用来指定页号的变量名
	 * @param integer $APageBundle 滚动栏显示的页数批示最大值
	 * @param boolean $IncJump 是否显示页面跳转下拉单(默认:true)
	 * @return string 页面滚动栏的HTML代码
	 */
	public static function get_page_bar($AScriptName,$ATotalCount,$ANowPageID,$AInnerCount,$AVar,$APageKey="Page",$APageBundle=9,$IncJump = false){
		//	没有项目
		if( $ATotalCount == 0 or $ATotalCount == "") return;//"No Item Here.";
		//	计算总页数
		if($AInnerCount<1)$AInnerCount=1;
		$PageCount = ceil($ATotalCount / $AInnerCount);
		//	检查控制当前页码值
		if($ANowPageID < 1) $ANowPageID = 1;
		if($ANowPageID > $PageCount) $ANowPageID = $PageCount;
		//	确定当然要显示的数码链接值
		$StartNum = ($ANowPageID - floor($APageBundle / 2)) > 0 ? ($ANowPageID - floor($APageBundle / 2)) : 1;
		$EndNum = ($PageCount - $StartNum) < $APageBundle ? $PageCount : ($StartNum + $APageBundle - 1);
		if((($EndNum - $StartNum) < $APageBundle)&&($PageCount > $APageBundle)) $StartNum = $EndNum - $APageBundle + 1;
		//	获得变量
		if(strlen($AVar) > 0) $Vars = explode("&",$AVar);
		if(count($Vars) > 0){
			for($i=0;$i<count($Vars);$i++){
				$v = explode("=",$Vars[$i]);
				if($v[0]!=$APageKey) $VarString.=sprintf("%s=%s&",$v[0],$v[1]);
			}
		}
		//	首页链接
		$OutStringr = sprintf('<a href="%s?%s%s=%s">%s</a>%s',$AScriptName,$VarString,$APageKey,"1","<<","\r");
		//	上一页链接
		$OutStringr .= ($ANowPageID==1) ? " " : sprintf('<a href="%s?%s%s=%s">%s</a>%s',$AScriptName,$VarString,$APageKey,$ANowPageID-1,"<","\r");
		//	中间页链接
		for($i = $StartNum; $i <= $EndNum; $i++)
		$OutStringr .= ($i==$ANowPageID) ? sprintf("[%s]%s",$i,"\r") : sprintf('<a href="%s?%s%s=%s">%s</a>%s',$AScriptName,$VarString,$APageKey,$i,$i,"\r");
		//	下一页链接
		$OutStringr .= ($ANowPageID==$PageCount) ? " " : sprintf('<a href="%s?%s%s=%s">%s</a>%s',$AScriptName,$VarString,$APageKey,$ANowPageID+1,">","\r");
		//	尾页链接
		//$OutStringr .= sprintf('<a href="%s?%s%s=%s">%s</a> 共%s页%s条%s',$AScriptName,$VarString,$APageKey,$PageCount,">>",$PageCount,$ATotalCount,"\r");
		$OutStringr .= sprintf('<a href="%s?%s%s=%s">%s</a> %s',$AScriptName,$VarString,$APageKey,$PageCount,">>","\r");
		//	跳转链接
		if ($IncJump){
//			$OutStringr .= sprintf("<label>跳至(J): <select name=\"ScrollJump\" accesskey=\"j\" onChange=\"javascript:if(this.value != 0) document.location='%s?%s%s='+this.value;\" id=\"ScrollJump\">%s",$$AScriptName,$VarString,$APageKey,"\r");
//			for($i = 1; $i <= $PageCount; $i++) {
//				$SelectedStr = ($i == $ANowPageID) ? " selected=\"selected\"" : "";
//				$OutStringr .= sprintf("<option value=\"%d\"%s>第%d页</option>%s",$i,$SelectedStr,$i,"\r");
//			}
//			$OutStringr .= "</select></label>";
		}
		//	输出
		return $OutStringr;
	}
}
?>